﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BusinessObjects;

namespace DataAccess.SqlServer
{
    public class SqlServerTinhTrang: ITinhTrang
    {
        public TinhTrang GetTinhTrang(int oid)
        {
            string sql = @" SELECT MaQuanLy, TenTinhTrang 
                            FROM TinhTrang
                            WHERE Oid = @oid";

            SqlParameter param = new SqlParameter("@oid", oid);

            DataRow row = DataProvider.GetDataRow(sql, param);

            string maQuanLy = row["MaQuanLy"].ToString();
            string tenTinhTrang = row["TenTinhTrang"].ToString();

            TinhTrang tinhtrang = new TinhTrang()
            {
                Oid = oid,
                MaQuanLy = maQuanLy,
                TenTinhTrang = tenTinhTrang
            };

            return tinhtrang;
        }

        public List<TinhTrang> GetListTinhTrang()
        {
            string sql = @" SELECT Oid, MaQuanLy, TenTinhTrang 
                            FROM TinhTrang";

            DataTable dt = DataProvider.GetDataTable(sql);

            List<TinhTrang> list = new List<TinhTrang>();

            foreach (DataRow row in dt.Rows)
            {
                int oid = int.Parse(row["Oid"].ToString());
                string maQuanLy = row["MaQuanLy"].ToString();
                string tenTinhTrang = row["TenTinhTrang"].ToString();

                TinhTrang tinhtrang = new TinhTrang()
                {
                    Oid = oid,
                    MaQuanLy = maQuanLy, 
                    TenTinhTrang = tenTinhTrang
                };

                list.Add(tinhtrang);
            }

            return list;
        }

        public int Insert(TinhTrang tinhTrang)
        {
            string sql = @" Insert into TinhTrang(MaQuanLy, TenTinhTrang)
                            Values(@MaQuanLy, @TenTinhTrang)";
            SqlParameter[] param = {
                                        new SqlParameter("@MaQuanLy", tinhTrang.MaQuanLy),
                                        new SqlParameter("@TenTinhTrang", tinhTrang.TenTinhTrang)
                                    };
            tinhTrang.Oid = DataProvider.Insert(sql, true, param);
            return tinhTrang.Oid;
        }

        public int Update(TinhTrang tinhTrang)
        {
            string sql = @" Update TinhTrang
                            Set MaQuanLy = @MaQuanLy, 
                                TenTinhTrang = @TenTinhTrang
                            Where Oid = @Oid";
            SqlParameter[] param = {
                                       new SqlParameter("@Oid", tinhTrang.Oid),
                                        new SqlParameter("@MaQuanLy", tinhTrang.MaQuanLy),
                                        new SqlParameter("@TenTinhTrang", tinhTrang.TenTinhTrang)
                                    };
            return DataProvider.Update(sql, param);
        }

        public int Delete(TinhTrang tinhTrang)
        {
            string sql = @" Delete From TinhTrang Where Oid = @Oid";
            SqlParameter param = new SqlParameter("@Oid", tinhTrang.Oid);
            return DataProvider.Update(sql, param);
        }


        public int Save(TinhTrang obj)
        {
            throw new NotImplementedException();
        }
    }
}
